package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PersonProxyService{

	@Autowired
	private PersonProxyDao personProxyDao;
	
	private QPersonProxy $ = QPersonProxy.personProxy;
	
	@Autowired
	public void setPersonProxyDao(PersonProxyDao personProxyDao) {
		this.personProxyDao = personProxyDao;
	}
	
	public List<PersonProxy> findAll(){
		return ListUtil.toList(personProxyDao.findAll());
	}
				
	public List<PersonProxy> findAll(
	Long personId,
	Long agentId,
	Date dateCreatedBegin,
	Date dateCreatedEnd,
	Date startDateBegin,
	Date startDateEnd,
	Date endDateBegin,
	Date endDateEnd,
	Date withdrawDateBegin,
	Date withdrawDateEnd,
	String status,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(IdUtil.isNotEmpty(personId)){
		condition.and($.person.id.eq(personId));
	}
	
	if(IdUtil.isNotEmpty(agentId)){
		condition.and($.agent.id.eq(agentId));
	}
	
	if(dateCreatedBegin != null){
		condition.and($.dateCreated.after(dateCreatedBegin));
	}
	
	if(dateCreatedEnd != null){
		condition.and($.dateCreated.before(dateCreatedEnd));
	}
	
	if(startDateBegin != null){
		condition.and($.startDate.after(startDateBegin));
	}
	
	if(startDateEnd != null){
		condition.and($.startDate.before(startDateEnd));
	}
	
	if(endDateBegin != null){
		condition.and($.endDate.after(endDateBegin));
	}
	
	if(endDateEnd != null){
		condition.and($.endDate.before(endDateEnd));
	}
	
	if(withdrawDateBegin != null){
		condition.and($.withdrawDate.after(withdrawDateBegin));
	}
	
	if(withdrawDateEnd != null){
		condition.and($.withdrawDate.before(withdrawDateEnd));
	}
	
	if(StringUtil.isNotEmpty(status)){
		condition.and($.status.contains(status));
	}
	
	
	return ListUtil.toList(personProxyDao.findAll(condition));
	}		

	public Page<PersonProxy> findAll(
			Long personId,
			Long agentId,
			Date dateCreatedBegin,
			Date dateCreatedEnd,
			Date startDateBegin,
			Date startDateEnd,
			Date endDateBegin,
			Date endDateEnd,
			Date withdrawDateBegin,
			Date withdrawDateEnd,
			String status,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(IdUtil.isNotEmpty(personId)){
				condition.and($.person.id.eq(personId));
			}
			if(IdUtil.isNotEmpty(agentId)){
				condition.and($.agent.id.eq(agentId));
			}
			if(dateCreatedBegin != null){
				condition.and($.dateCreated.after(dateCreatedBegin));
			}
			
			if(dateCreatedEnd != null){
				condition.and($.dateCreated.before(dateCreatedEnd));
			}
			if(startDateBegin != null){
				condition.and($.startDate.after(startDateBegin));
			}
			
			if(startDateEnd != null){
				condition.and($.startDate.before(startDateEnd));
			}
			if(endDateBegin != null){
				condition.and($.endDate.after(endDateBegin));
			}
			
			if(endDateEnd != null){
				condition.and($.endDate.before(endDateEnd));
			}
			if(withdrawDateBegin != null){
				condition.and($.withdrawDate.after(withdrawDateBegin));
			}
			
			if(withdrawDateEnd != null){
				condition.and($.withdrawDate.before(withdrawDateEnd));
			}
			if(StringUtil.isNotEmpty(status)){
				condition.and($.status.contains(status));
			}
	
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject);
			return personProxyDao.findAll(condition, pageRequest);
	}
	
	
	
	public PersonProxy getPersonProxy(Long id){
		return personProxyDao.getOne(id);
	}
	
	public void deletePersonProxy(Long id){
		personProxyDao.delete(id);
	}

	public void savePersonProxy(PersonProxy personProxy) {
		if (null == personProxy.getDateCreated()) {
			personProxy.setDateCreated(DateUtil.nowDate());
		}
		personProxyDao.save(personProxy);
	}

	public PersonProxy findOne(Long id) {
		return this.findOne(id);
	}

}
		
